﻿<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="Rezepto.Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script type="text/javascript">
        var originsrc = new Array();
        var bildnumber = 0;
    function SetRatingIcons(img) {
        if (originsrc.length == 0) {
            for (var i = 1; i <= 5; i++) {
                originsrc[i] = document.getElementById("MainContent_Bewertung" + i).src;
            }
        }
        var rating = parseInt(img.charAt(img.length - 1));
        for (var i = 1; i <= rating; i++) {
            document.getElementById("MainContent_Bewertung" + i).src = "/Pics/löffel-goldkl.png";
        }
        var start = rating + 1;
        for (var tmp = start; tmp <= 5; tmp++) {
            document.getElementById("MainContent_Bewertung" + tmp).src = "/Pics/löffel-graukl.png";
        }
    }
    function ResetRatingIcons() {
        for (var i = 1; i <= 5; i++) {
            document.getElementById("MainContent_Bewertung" + i).src = originsrc[i];
        }
    }

    function BuildRatingIcons(rating) {
        halfRated = 1;
        if (rating % 1 === 0) {
            halfRated = 0;
        };
        var goodRating = rating - halfRated;
        var badRating = 5 - rating - halfRated;
        sumrating = 1;
        for (var good = 0; good < goodRating; good++) {
            document.getElementById("MainContent_Bewertung" + sumrating).src = "/Pics/löffel-goldkl.png";
            originsrc[sumrating] = "/Pics/löffel-goldkl.png";
            sumrating++;
        }
        for (var half = 0; half < halfRated; half++) {
            document.getElementById("MainContent_Bewertung" + sumrating).src = "/Pics/löffel-halfkl.png";
            originsrc[sumrating] = "/Pics/löffel-halfkl.png";
            sumrating++;
        }
        for (var bad = 0; bad < badRating; bad++) {
            document.getElementById("MainContent_Bewertung" + sumrating).src = "/Pics/löffel-graukl.png";
            originsrc[sumrating] = "/Pics/löffel-graukl.png";
            sumrating++;
        }
    }

    function GetNextPicture() {
        var xmlhttp;
        var rezeptid = document.getElementById("MainContent_RezeptIdFeld").innerHTML;
        var maxBilder = parseInt(document.getElementById("MainContent_BilderGesamt").innerHTML);
        if (bildnumber >= (maxBilder-1)) {
            bildnumber = 0;
        } else {
            bildnumber++;
        }
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                if (xmlhttp.responseText != "") {
                    document.getElementById("MainContent_RezeptBilder").src = "";
                    document.getElementById("MainContent_RezeptBilder").src = xmlhttp.responseText;                    
                }
            }
        };
        xmlhttp.open("GET", "NextPictureAjax.aspx?rezeptid=" + rezeptid + "&bildnumber=" + bildnumber, true);
        xmlhttp.send();
    }

    function RateRecipeAndUpdateRating(img) {
        var xmlhttp;
        var originRating = document.getElementById("MainContent_RatingValue").innerHTML;
        var rezeptid = document.getElementById("MainContent_RezeptIdFeld").innerHTML;
        var userid = document.getElementById("MainContent_UserIdFeld").innerHTML;
        var rating = parseInt(img.charAt(img.length - 1));
        if (userid == "") {
            alert("Du musst dich einloggen um abzustimmen.");
            return;
        }
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                if (xmlhttp.responseText.indexOf("bereits") != -1) {
                    document.getElementById("MainContent_RatingValue").innerHTML = originRating;
                } else {
                    document.getElementById("MainContent_RatingValue").innerHTML = "(" + xmlhttp.responseText + ")";
                    BuildRatingIcons(parseFloat(xmlhttp.responseText.replace(",",".")));
                }
                
            }
        };
        xmlhttp.open("GET", "AjaxRating.aspx?rating=" + rating + "&rezeptid=" + rezeptid + "&userid=" + userid, true);
        xmlhttp.send();
    }
</script>    
    <h2>
        <asp:Label runat="server" ID="BegruessungText"></asp:Label><asp:Label runat="server" ID="RezeptIdFeld"></asp:Label>
        <asp:Label runat="server" ID="UserIdFeld"></asp:Label><asp:Label runat="server" ID="BilderGesamt"></asp:Label><asp:Label runat="server" ID="AktuellesBild"></asp:Label>
    </h2>
        <asp:Panel runat="server" ID="RezeptPanel">
            <h2><b><asp:Label runat="server" ID="RezeptName"></asp:Label></b><asp:PlaceHolder runat="server" ID="BewertungPlaceHolder"></asp:PlaceHolder></h2>
            <div id="bildzutaten" style="width: 918px; height: 300px;"><asp:Image CssClass="RezeptBild" ID="RezeptBilder" runat="server" Height="300px" Width="400px" ImageAlign="Left" />
            <div style="padding-left: 40px;"><h3><b><asp:Label ID="ZutatenHeader" runat="server"></asp:Label></b></h3><p>
                <asp:Table ID="ZutatenTable" runat="server" HorizontalAlign="Left"></asp:Table>
                <asp:Table ID="ZutatenTable2" runat="server"></asp:Table></p></div></div>
            <p><h3><b><asp:Label ID="ZubereitungHeader" runat="server"></asp:Label></b></h3></p>
            <p><asp:Label ID="RezeptZubereitung" runat="server"></asp:Label></p>
            <h3><b><asp:Label ID="BildUpload" runat="server" Visible="False">Bild hochladen</asp:Label></b></h3>
            <asp:FileUpload ID="FileUpload" runat="server" Visible="False"/><asp:Button ID="BildUploadButton" runat="server" OnClick="SavePicture" 
                Text="Bild hochladen" Visible="False" />
            <h3><b><asp:Label ID="KommentarHeader" runat="server"></asp:Label></b></h3>
            <p><b><asp:Table ID="RezeptKommentare" runat="server"></asp:Table></b></p>
            <asp:TextBox ID="Kommentar" runat="server" Height="200" MaxLength="2500" 
                TextMode="MultiLine" Visible="False" Width="600"></asp:TextBox>
            <asp:Button ID="SaveCommentaryButton" runat="server" OnClick="SaveCommentary" 
                Text="Kommentar speichern" Visible="False" />
        </asp:Panel>
</asp:Content>
